package com.tee.service.privilege.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tee.bean.privilege.Department;
import com.tee.bean.privilege.Role;
import com.tee.bean.privilege.User;
import com.tee.service.base.DaoSupport;
import com.tee.service.privilege.UserService;
import com.tee.utils.MD5;


@Service
@Transactional
public class UserServiceBean extends DaoSupport<User> implements UserService {

	public void setVisibleStatu(Integer[] ids, boolean statu) {
		if (ids != null && ids.length > 0) {
			StringBuffer jpql = new StringBuffer();
			for (int i = 0; i < ids.length; i++) {
				jpql.append('?').append((i + 2)).append(',');
			}
			jpql.deleteCharAt(jpql.length() - 1);
			Query query = em
					.createQuery("update User o set o.visible=?1 where o.id in("
							+ jpql.toString() + ")");
			query.setParameter(1, statu);
			for (int i = 0; i < ids.length; i++) {
				query.setParameter(i + 2, ids[i]);
			}
			query.executeUpdate();
		}
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public boolean exsit(String phone) {
		long count = (Long) em.createQuery(
				"select count(o) from User o where o.phone=?1").setParameter(
				1, phone).getSingleResult();
		return count > 0;
	}

	/**
	 * 修改密码
	 */
	public void updatePassword(String phone, String newpassword) {
		em.createQuery("update User o set o.password=?1 where o.phone=?2")
				.setParameter(1, MD5.MD5Encode(newpassword)).setParameter(2,
						phone).executeUpdate();

	}

	public boolean checkUser(String phone, String password) {
		long count;
		count = (Long) em
				.createQuery("select count(o) from User o where o.phone=?1 and o.password=?2")
				.setParameter(1, phone).setParameter(2,
						MD5.MD5Encode(password)).getSingleResult();
		return count > 0;
	}

	@Override
	public void delete(Serializable... entityids) {
		super.delete(entityids);
	}

	@Override
	public void save(Object entity) {
		User user = (User) entity;
		user.setPassword(MD5.MD5Encode(user.getPhone()));
		super.save(entity);
	}

	public User findByPhone(String phone) {
		return (User) em.createQuery("select o from User o where o.phone=?1")
				.setParameter(1, phone).getSingleResult();
	}

	/**
	 * 通过部门查询部门下面的可用人员
	 */
	@SuppressWarnings("unchecked")
	public List<User> getUsersByDepartment(Department department) {
		return (List<User>) em.createQuery(
				"select o from User o where o.department=?1 and o.visible=?2").setParameter(1,
				department).setParameter(2, true).getResultList();
	}


	@SuppressWarnings("unchecked")
	public List<User> getByRole(Role role){
		return em.createQuery("from User u where u.role=?1").setParameter(1, role).getResultList();
		
	}
	@SuppressWarnings("unchecked")
	public List<User> getUserByDirector(Integer directorid){
		return em.createQuery("from User u where u.director.id=?1").setParameter(1, directorid).getResultList();
	}
}
